﻿Imports System
Imports SlimDX
Imports SlimDX.Direct3D11
Imports SlimDX.Direct3D9
Imports System.Runtime.InteropServices

Namespace Engine

    Public Class Room

        Inherits Entity
        Implements IDisposable

#Region "Members"

        Private meshFilename As String = BasePathResource & "\Resources\Models\room.x"
        Private textureFilename As String = BasePathResource & "\Resources\Textures\rockwall.dds"
        Private textureFilename1 As String = BasePathResource & "\Resources\Textures\rockwall_normal.dds"
        Private effectFilename As String = BasePathResource & "\Resources\Shaders\bump.fx"
        Private texBump As Texture



#End Region
#Region "Methods"



        Public Overrides Sub Init(ByRef mDevice As Direct3D9.Device)

            LoadObject(mDevice, meshFilename, effectFilename)



            'public Room(Device device) : base(device, meshFilename, effectFilename)
            '{
            'load the texture

            texture = texture.FromFile(mDevice, textureFilename)
            texBump = texture.FromFile(mDevice, textureFilename1)
        End Sub

        Public Overrides Sub Update(_matView As Matrix, _matProj As Matrix)
            matWorld = Matrix.Translation(0.0!, 0.0!, 0.0!)
            Me.matView = _matView
            Me.matProj = _matProj
        End Sub

        Public Overrides Sub Render(ByRef Device As Direct3D9.Device)

            ' Calculate the matrices
            Dim modelViewProj As Matrix = matWorld * matView * matProj
            Dim modelViewIT As Matrix = matWorld * matView * matProj

            modelViewIT = Matrix.Invert(modelViewIT)
            modelViewIT = Matrix.Transpose(modelViewIT)

            'set the technique
            meffect.Technique = "bump"
            'set the texturs
            meffect.SetTexture(New EffectHandle("texture0"), texture)
            meffect.SetTexture(New EffectHandle("texture1"), texBump)

            'set the matrices
            meffect.SetValue("ModelViewProj", modelViewProj)
            meffect.SetValue("ModelViewIT", modelViewIT)
            meffect.SetValue("ModelWorld", matWorld)
            ' set the light position
            Dim time As Double = (Environment.TickCount * 0.001)
            meffect.SetValue(New EffectHandle("lightPos"), New Vector4((300 * Math.Sin(time)), 40.0!, (300 * Math.Cos(time)), 1.0!))

            ' render the effect
            meffect.Begin(0)
            meffect.BeginPass(0)

            mesh.DrawSubset(0)
            meffect.EndPass()
            meffect.End()
        End Sub
#End Region

        Public Sub Dispose1() Implements IDisposable.Dispose

        End Sub
    End Class


    Public Class Water
        Inherits Entity
        Implements IDisposable
#Region "Members"

        Private meshFilename As String = BasePathResource & "\Resources\Models\water.x"
        Private textureFilename As String = BasePathResource & "\Resources\Textures\bricks.dds"
        Private textureFilename1 As String = BasePathResource & "\Resources\Textures\water.dds"
        Private effectFilename As String = BasePathResource & "\Resources\Shaders\water.fx"


        Private texCube As CubeTexture

        Private Shared waterTime As Single = 0
#End Region
#Region "Methods"

        Public Overrides Sub Init(ByRef mDevice As Direct3D9.Device)
            LoadObject(mDevice, meshFilename, effectFilename)
            texCube = CubeTexture.FromFile(mDevice, textureFilename)
        End Sub

     

        Public Overrides Sub Update(ByVal matView As Matrix, ByVal matProj As Matrix)
            matWorld = Matrix.Translation(0.0!, -80.0!, 0.0!)
            Me.matView = matView
            Me.matProj = matProj
        End Sub

        Public Overrides Sub Render(ByRef mDevice As Direct3D9.Device)
            Dim time As Single = CType((Environment.TickCount * 0.001), Single)
            waterTime = (waterTime + 0.002!)
            'Calculate the matrices
            Dim modelViewProj As Matrix = (matWorld _
                        * (matView * matProj))
            Dim modelViewIT As Matrix = (matWorld _
                        * (matView * matProj))
            modelViewIT.Invert()
            modelViewIT = Matrix.Transpose(modelViewIT)
            'set the technique
            meffect.Technique = "water"
            'set the texturs
            
            meffect.SetTexture(New EffectHandle("texture0"), texture)
            meffect.SetTexture(New EffectHandle("texture1"), texCube)
            'set the matrices
            meffect.SetValue("ModelViewProj", modelViewProj)
            meffect.SetValue("ModelWorld", matWorld)
            'set eye position
            meffect.SetValue("eyePos", New Vector4(450.0!, 250.0!, 750.0!, 1.0!))
            'set the light position
            meffect.SetValue("lightPos", New Vector4(CType((300 * Math.Sin(time)), Single), 40.0!, CType((300 * Math.Cos(time)), Single), 1.0!))
            'set the time
            meffect.SetValue("time", waterTime)
            'render the effect
            mDevice.SetRenderState(RenderState.AlphaBlendEnable, True)
            meffect.Begin(0)
            meffect.BeginPass(0)
            mesh.DrawSubset(0)
            meffect.EndPass()
            meffect.End()
            mDevice.SetRenderState(RenderState.AlphaBlendEnable, False)
        End Sub
        Public Sub Dispose1() Implements IDisposable.Dispose

        End Sub
#End Region
    End Class

    Public Class Fountain
        Inherits Entity
        Implements IDisposable
#Region "Members"
        Private meshFilename As String = BasePathResource & "\Resources\Models\fountain.x"
        Private textureFilename As String = BasePathResource & "\Resources\Textures\rockwall.dds"
        Private textureFilename1 As String = BasePathResource & "\Resources\Textures\rockwall_normal.dds"
        Private effectFilename As String = BasePathResource & "\Resources\Shaders\bump.fx"
        Private texBump As Texture

#End Region
#Region "Methods"

        Public Overrides Sub Init(ByRef mdevice As Direct3D9.Device)
            LoadObject(mdevice, meshFilename, effectFilename)
            'load the texture
            texture = texture.FromFile(mdevice, textureFilename)
            texBump = texture.FromFile(mdevice, textureFilename1)
        End Sub

        Public Overrides Sub Update(ByVal matView As Matrix, ByVal matProj As Matrix)
            matWorld = Matrix.Translation(0.0!, -800.0!, 0.0!)
            Me.matView = matView
            Me.matProj = matProj
        End Sub

        Public Overrides Sub Render(ByRef mdevice As Direct3D9.Device)
            'Calculate the matrices
            Dim modelViewProj As Matrix = (matWorld _
                        * (matView * matProj))
            Dim modelViewIT As Matrix = (matWorld _
                        * (matView * matProj))
            modelViewIT = Matrix.Invert(modelViewIT)
            modelViewIT = Matrix.Transpose(modelViewIT)
            'set the technique
            meffect.Technique = "bump"
            'set the texturs
            meffect.SetTexture(New EffectHandle("texture0"), texture)
            meffect.SetTexture(New EffectHandle("texture1"), texBump)
            'set the matrices
            meffect.SetValue("ModelViewProj", modelViewProj)
            meffect.SetValue("ModelViewIT", modelViewIT)
            meffect.SetValue("ModelWorld", matWorld)
            'set the light position
            Dim time As Single = CType((Environment.TickCount * 0.001), Single)
            meffect.SetValue("lightPos", New Vector4(CType((300 * Math.Sin(time)), Single), 40.0!, CType((300 * Math.Cos(time)), Single), 1.0!))
            'render the effect
            meffect.Begin(0)
            meffect.BeginPass(0)
            mesh.DrawSubset(0)
            meffect.EndPass()
            meffect.End()
        End Sub
        Public Sub Dispose1() Implements IDisposable.Dispose

        End Sub
#End Region

     
    End Class


    Public Class SimpleTest

        Inherits Entity

#Region "Members"

     

        Private Structure Vertex

            Public PositionRhw As Vector4
            Public Color As Integer

            Public Function SizeBytes() As Integer
                Return System.Runtime.InteropServices.Marshal.SizeOf(Me)
            End Function

            Public Function Format() As VertexFormat
                Return VertexFormat.PositionRhw Or VertexFormat.Diffuse
            End Function

        End Structure

        Private vs As Vertex

        Public Vertices As SlimDX.Direct3D9.VertexBuffer

#End Region
#Region "Methods"

      

        Private Function BuildVertexData() As Vertex()

            Dim vertexData(2) As Vertex


            vertexData(0).PositionRhw = New Vector4(400.0!, 100.0!, 0.5F, 1.0!)
            vertexData(0).Color = System.Drawing.Color.Red.ToArgb()

            vertexData(1).PositionRhw = New Vector4(650.0!, 500.0!, 0.5F, 1.0!)
            vertexData(1).Color = System.Drawing.Color.Blue.ToArgb()

            vertexData(2).PositionRhw = New Vector4(150.0!, 500.0!, 0.5F, 1.0!)
            vertexData(2).Color = System.Drawing.Color.Green.ToArgb()

            Return vertexData
        End Function

        Public Overrides Sub Init(ByRef mDevice As Direct3D9.Device)
            ' Build Data
            ' just debug 1 triangle

            Vertices = New VertexBuffer(mDevice, 3 * vs.SizeBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed)
            Dim stream As DataStream = Vertices.Lock(0, 0, LockFlags.None)
            stream.WriteRange(BuildVertexData())
            Vertices.Unlock()
        End Sub

        Public Overrides Sub Update(matView As Matrix, matProj As Matrix)
            matWorld = Matrix.Translation(0.0!, 0.0!, 0.0!)
            matView = matView
            matProj = matProj
        End Sub

        Public Overrides Sub Render(ByRef Device As Direct3D9.Device)

            ' Calculate the matrices
            Dim modelViewProj As Matrix = matWorld * matView * matProj
            Dim modelViewIT As Matrix = matWorld * matView * matProj

            modelViewIT = Matrix.Invert(modelViewIT)
            modelViewIT = Matrix.Transpose(modelViewIT)


            With Device
                .SetStreamSource(0, Vertices, 0, vs.SizeBytes)
                .VertexFormat = vs.Format
                .DrawPrimitives(PrimitiveType.TriangleList, 0, 1)
            End With

        End Sub
#End Region
    End Class
    Public Module SharedMembers
        Public BasePathResource As String
    End Module

End Namespace